﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;

namespace DataAccess.Operate
{
    public class DA_SoftWare_Operate:SqlInteractionBase
    {
        private DataEntity.T_SoftWare _objMessage = null;

        public override bool Insert(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_SoftWare_Insert]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = (DataEntity.T_SoftWare)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidSoftWare_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_Name", SqlDbType.NVarChar, 200, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_Name));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_Keywords", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_Keywords));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_Description1", SqlDbType.NVarChar, -1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_Description1));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_Description2", SqlDbType.NVarChar, -1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_Description2));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_NetsiskAddress", SqlDbType.NVarChar, 300, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_NetsiskAddress));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_WebsiteAddress", SqlDbType.NVarChar, 300, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_WebsiteAddress));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_RegName", SqlDbType.NVarChar, 200, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_RegName));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_RegKey", SqlDbType.NVarChar, 300, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_RegKey));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_Remarks", SqlDbType.NVarChar, -1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_Remarks));
                cmdToExecute.Parameters.Add(new SqlParameter("@iSoftWare_Rating", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_Rating));
                cmdToExecute.Parameters.Add(new SqlParameter("@iSoftWare_IsOk", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_IsOk));
                cmdToExecute.Parameters.Add(new SqlParameter("@iSoftWare_IsSecure", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_IsSecure));
                cmdToExecute.Parameters.Add(new SqlParameter("@daSoftWare_DateTime", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_DateTime));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_SoftWare::Insert::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override bool Update(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_SoftWare_Update]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = (DataEntity.T_SoftWare)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidSoftWare_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_Name", SqlDbType.NVarChar, 200, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_Name));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_Keywords", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_Keywords));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_Description1", SqlDbType.NVarChar, -1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_Description1));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_Description2", SqlDbType.NVarChar, -1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_Description2));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_NetsiskAddress", SqlDbType.NVarChar, 300, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_NetsiskAddress));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_WebsiteAddress", SqlDbType.NVarChar, 300, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_WebsiteAddress));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_RegName", SqlDbType.NVarChar, 200, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_RegName));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_RegKey", SqlDbType.NVarChar, 300, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_RegKey));
                cmdToExecute.Parameters.Add(new SqlParameter("@sSoftWare_Remarks", SqlDbType.NVarChar, -1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_Remarks));
                cmdToExecute.Parameters.Add(new SqlParameter("@iSoftWare_Rating", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_Rating));
                cmdToExecute.Parameters.Add(new SqlParameter("@iSoftWare_IsOk", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_IsOk));
                cmdToExecute.Parameters.Add(new SqlParameter("@iSoftWare_IsSecure", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_IsSecure));
                cmdToExecute.Parameters.Add(new SqlParameter("@daSoftWare_DateTime", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.SoftWare_DateTime));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_SoftWare::Update::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override bool Delete(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_SoftWare_Delete]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;

            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidSoftWare_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, SqlGuid.Parse(obj.ToString())));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_SoftWare::Delete::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override object SelectOne(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_SoftWare_SelectOne]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;
            DataTable toReturn = new DataTable("t_SoftWare");
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;

            try
            {
                this._objMessage = new DataEntity.T_SoftWare();
                cmdToExecute.Parameters.Add(new SqlParameter("@guidSoftWare_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, SqlGuid.Parse(obj.ToString())));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                adapter.Fill(toReturn);
                if (toReturn.Rows.Count > 0)
                {
                    _objMessage.SoftWare_ID = (Guid)toReturn.Rows[0]["SoftWare_ID"];
                    _objMessage.SoftWare_Name = (string)toReturn.Rows[0]["SoftWare_Name"];
                    _objMessage.SoftWare_Keywords = toReturn.Rows[0]["SoftWare_Keywords"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["SoftWare_Keywords"];
                    _objMessage.SoftWare_Description1 = toReturn.Rows[0]["SoftWare_Description1"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["SoftWare_Description1"];
                    _objMessage.SoftWare_Description2 = toReturn.Rows[0]["SoftWare_Description2"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["SoftWare_Description2"];
                    _objMessage.SoftWare_NetsiskAddress = toReturn.Rows[0]["SoftWare_NetsiskAddress"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["SoftWare_NetsiskAddress"];
                    _objMessage.SoftWare_WebsiteAddress = toReturn.Rows[0]["SoftWare_WebsiteAddress"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["SoftWare_WebsiteAddress"];
                    _objMessage.SoftWare_RegName = toReturn.Rows[0]["SoftWare_RegName"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["SoftWare_RegName"];
                    _objMessage.SoftWare_RegKey = toReturn.Rows[0]["SoftWare_RegKey"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["SoftWare_RegKey"];
                    _objMessage.SoftWare_Remarks = toReturn.Rows[0]["SoftWare_Remarks"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["SoftWare_Remarks"];
                    _objMessage.SoftWare_Rating = toReturn.Rows[0]["SoftWare_Rating"] == System.DBNull.Value ? SqlInt32.Null : (Int32)toReturn.Rows[0]["SoftWare_Rating"];
                    _objMessage.SoftWare_IsOk = toReturn.Rows[0]["SoftWare_IsOk"] == System.DBNull.Value ? SqlInt32.Null : (Int32)toReturn.Rows[0]["SoftWare_IsOk"];
                    _objMessage.SoftWare_IsSecure = toReturn.Rows[0]["SoftWare_IsSecure"] == System.DBNull.Value ? SqlInt32.Null : (Int32)toReturn.Rows[0]["SoftWare_IsSecure"];
                    _objMessage.SoftWare_DateTime = toReturn.Rows[0]["SoftWare_DateTime"] == System.DBNull.Value ? SqlDateTime.Null : (DateTime)toReturn.Rows[0]["SoftWare_DateTime"];
                }
                return this._objMessage;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_SoftWare::SelectOne::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
                adapter.Dispose();
            }
        }


        public override DataSet SelectAll()
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_SoftWare_SelectAll]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;

            try
            {

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                adapter.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_SoftWare::SelectAll::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
                adapter.Dispose();
            }
        }

    }
}
